package sist.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import sist.bean.AdminMemberBean;
import sist.bean.CompanyBean;
import sist.dao.MyBatisDao;

@Controller
@Component
public class MyBatisCrudController {
	@Resource(name="companyDao")
	private MyBatisDao dao;
	
	@RequestMapping(value="/view/companyInsert.do")
	public String insertCompany(CompanyBean bean)throws Throwable{
		System.out.println(bean);
		dao.insertCompany(bean);
		return "redirect:/view/companyList.do";
	}
	
	@RequestMapping(value="/view/companyList.do")
	public ModelAndView selectCompany(CompanyBean bean,HttpSession session)throws Throwable{
		ModelAndView mv=new ModelAndView();
		System.out.println("selectCompany");
		
		if(session.getAttribute("info")!=null){
			mv.addObject("info", session.getAttribute("info"));
			session.invalidate();
		}
		
		mv.addObject("list", dao.selectCompany(bean));
		if(session.getAttribute("adBean")!=null){
			mv.setViewName("/view/companyEdit");
		}else{
			mv.setViewName("redirect:/view/adminLogin.jsp");
		}
		
		return mv;
	}
	
	@RequestMapping(value="/view/info.do")
	public ModelAndView selectCompanyInfo(@RequestParam(value="no")int no,HttpSession session)throws Throwable{
		ModelAndView mv=new ModelAndView();
		System.out.println("selectCompanyInfo:"+no);
		//mv.addObject("info",dao.selectCompanyInfo(no));
		session.setAttribute("info", dao.selectCompanyInfo(no));
		mv.setViewName("redirect:/view/companyList.do");
		return mv;
	}
	
	@RequestMapping(value="/view/updateCompany.do")
	public String updateCompany(CompanyBean bean)throws Throwable{
		System.out.println(bean);
		dao.updateCompany(bean);
		return "redirect:/view/companyList.do"; 
	}
	
	@RequestMapping(value="/view/deleteCompany.do")
	public String deleteCompany(@RequestParam(value="no")int no)throws Throwable{
		System.out.println(no);
		dao.deleteCompany(no);
		return "redirect:/view/companyList.do";
	}
	
	@RequestMapping(value="/view/adminLogin.do")
	public ModelAndView adminLogin(AdminMemberBean bean,HttpSession session)throws Throwable{
		ModelAndView mv=new ModelAndView();
		AdminMemberBean dbAdBean=dao.adminLogin(bean);
		if(dbAdBean!=null&&bean.getPassword().equals(dbAdBean.getPassword())){
			session.setAttribute("adBean", dbAdBean);
			mv.setViewName("redirect:/view/companyList.do");
		}else{
			mv.setViewName("redirect:/view/adminLogin.jsp");
		}
		return mv;
	}
	
	@RequestMapping(value="/htmlView/form1Text.do")
	public String form1Text(@RequestParam(value="name",required=false)String name,@RequestParam(value="age",required=false)String age)throws Throwable{
		System.out.println(name+" "+age);
		return "/htmlView/getView";
	}
	
	@RequestMapping(value="/htmlView/form2Text.do")
	public String form2Text(@RequestParam(value="name",required=false)String name,@RequestParam(value="age",required=false)String age)throws Throwable{
		System.out.println(name+" "+age);
		return "/htmlView/postView";
	}
}
